package com.neegix.development.api.application.service.impl;

import com.neegix.application.command.BaseService;
import com.neegix.development.api.application.dto.ApiDTO;
import com.neegix.exception.BusinessRuntimeException;
import com.neegix.development.api.application.cqrs.command.BatchDeleteApiCommand;
import com.neegix.development.api.application.cqrs.command.NewApiCommand;
import com.neegix.development.api.application.cqrs.command.UpdateApiCommand;
import com.neegix.development.api.application.cqrs.query.ApiQueryRepository;
import com.neegix.development.api.application.service.ApiService;
import com.neegix.development.api.domain.entity.ApiEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-11-22 11:43:14
  */

@Service
public class ApiServiceImpl extends BaseService implements ApiService{
    @Autowired
    private ApiQueryRepository apiQueryRepository;

    @Override
    public Void createApi(ApiEntity apiEntity) {
        Optional<ApiDTO> optional = apiQueryRepository.findByAccess(apiEntity.getAccess());
        if (optional.isPresent()){
            throw new BusinessRuntimeException("菜单["+apiEntity.getName()+ "]已存在！");
        }
        return commandInvoker.execute(new NewApiCommand(apiEntity));
    }

    @Override
    public Void modifyApi(ApiEntity apiEntity) {
        return commandInvoker.execute(new UpdateApiCommand(apiEntity));
    }

    @Override
    public Void removeApi(List<Long> ids) {
        Integer count = apiQueryRepository.selectCount(ids);
        if (count.equals(0)){
            throw new BusinessRuntimeException("要删除的记录不存在！");
        }
        return commandInvoker.execute(new BatchDeleteApiCommand(ids));
    }
}
